﻿-- Author      : Kurapica

--------------------------
-- Selector                 --
--------------------------
do    
    local getmetatable = getmetatable
    local pairs = pairs
    local ipairs = ipairs
    local type = type
    local error = error
    local max = max
    local min = min
    local abs = abs
    local floor = floor
    local CreateFrame = IGAS.GUI.CreateFrame
    
    -- Base Form for all other forms in Studio    
    local _WidgetName = "Selector"
    local _Base = IGAS.GUI.Widget.Frame
    
    local _Widget = IGAS.Studio.Widget
    local _PropertyType = IGAS.GUI.Base.PropertyType
    
    -- Script
    -- Only one selector would be created.
    local selector
    local stF
    local stX, stY, endX, endY
    
    local _FrameBackdrop = {
		edgeFile = "Interface\\Tooltips\\UI-Tooltip-Border",
		tile = true, tileSize = 16, edgeSize = 16,
		insets = { left = 3, right = 3, top = 3, bottom = 3 }
	}
    
    local function OnUpdate(self, elasped)
        endX, endY = GetCursorPosition()
        if abs(stX-endX) > 6 or abs(stY - endY) > 6 then
            selector:SetBackdrop(_FrameBackdrop)
        end
        if selector.Visible then
            selector:SetPoint("TOPRIGHT", selector.Parent, "BOTTOMLEFT", max(stX, endX), max(stY, endY))
            selector:SetPoint("BOTTOMLEFT", selector.Parent, "BOTTOMLEFT", min(stX, endX), min(stY, endY))
        end
    end
    
    local function OnMouseDown(self, ...)
		if not selector.__WidgetList or not selector.__WidgetList.SelectedWidget then
			return
		end
        stF = self
        stX, stY = GetCursorPosition()
        endX, endY = stX, stY
        selector:SetBackdrop(nil)
        selector.OnUpdate = OnUpdate
        selector.Visible = true
    end
    
    local function OnMouseUp(self, ...)
		if not selector.__WidgetList or not selector.__WidgetList.SelectedWidget then
			return
		end
        selector.OnUpdate = nil
        selector.Visible = false
        if abs(stX-endX) > 6 or abs(stY - endY) > 6 then
            selector:Fire("OnAreaSelected", stF, floor(min(stX, endX)), floor(max(stY, endY)), floor(max(stX, endX)), floor(min(stY, endY)))
        end
    end
	
    -- ScriptType
    local _ScriptType = {
        ["OnAreaSelected"] = true,        
    }
    
    --    FuncProxy
    local _FuncProxy = {
        -- HookFrame
        ["HookFrame"] = function(self, frame)
			frame.__UI:EnableMouse(true)
            frame:StackScript("OnMouseDown", OnMouseDown)
            frame:StackScript("OnMouseUp", OnMouseUp)
        end,
    }
    
    --    Property
    local _Property = {
    }
    
    local function _New(parent, wList)
		-- Only One selector will be exits, so....
        if selector then
            return selector
        end
        
        selector = CreateFrame(_Base, nil, parent)
        selector.__WidgetList = wList
        selector.TopLevel = true
        selector:SetBackdrop(_FrameBackdrop)
        
        return selector
    end    
    
    -- Register
    local _WidgetInfo = {
        ["WidgetName"] = _WidgetName,
        ["Base"] = _Base,
        ["FuncProxy"] = _FuncProxy,
        ["Property"] = _Property,
        ["ScriptType"] = _ScriptType,
        ["New"] = _New,
    }
    
    _Widget[_WidgetName] = IGAS:NewWidget(_WidgetInfo)
end
